package lumis.upgrade;

import java.sql.Timestamp;

import lumis.portal.PortalException;
import lumis.util.XmlUtil;

import org.w3c.dom.Node;

/**
 * Stores information about a upgrade build.
 * 
 * @since 4.0.0
 * @version $Revision: 8289 $ $Date: 2007-09-24 17:46:32 -0300 (Mon, 24 Sep 2007) $
 */
public class BuildConfig
{
	public static final int DEFAULT_MAJOR_VERSION = 4;
	public static final int DEFAULT_MINOR_VERSION = 0;
	public static final int DEFAULT_RELEASE_VERSION = 0;
	public static final String DEFAULT_BUILD_NUMBER = "050101";
	public static final int DEFAULT_BUILD_SEQUENCE = 0;
	public static final int DEFAULT_STEPS_NUMBER = 1;
	
	private String id;
	private int majorVersion = BuildConfig.DEFAULT_MAJOR_VERSION;
	private int minorVersion = BuildConfig.DEFAULT_MINOR_VERSION;
	private int releaseVersion = BuildConfig.DEFAULT_RELEASE_VERSION;
	private String buildNumber = BuildConfig.DEFAULT_BUILD_NUMBER;
	private int buildSequence = BuildConfig.DEFAULT_BUILD_SEQUENCE;
	private String message = null;
	private String messageType = null;
	private String area = null;
	private Timestamp upgradeDate;
	private int steps = DEFAULT_STEPS_NUMBER;
	
	public BuildConfig()
	{
	}

	/**
	 * Creates a BuildConfig initializing its properties from the given
	 * build xml node.
	 * @param buildNode the build's xml node.
	 * @since 4.0.11
	 */
	public BuildConfig(Node buildNode) throws PortalException
	{
		if (buildNode == null)
			throw new IllegalArgumentException("'buildNode' cannot be null");
		
		setMajorVersion(Integer.parseInt(XmlUtil.readAttributeString("majorVersion", buildNode)));
		setMinorVersion(Integer.parseInt(XmlUtil.readAttributeString("minorVersion", buildNode)));
		setReleaseVersion(Integer.parseInt(XmlUtil.readAttributeString("releaseVersion", buildNode)));
		setBuildNumber(XmlUtil.readAttributeString("buildNumber", buildNode));
		setBuildSequence(Integer.parseInt(XmlUtil.readAttributeString("buildSequence", buildNode)));
		setArea(XmlUtil.readAttributeString("area", buildNode, ""));
		
		Node[] stepNodes = XmlUtil.selectNodes("steps/step", buildNode);
		setSteps(stepNodes.length);
	}

	/**
	 * @return Returns the majorVersion.
	 */
	public int getMajorVersion()
	{
		return majorVersion;
	}
	
	/**
	 * @return Returns the minorVersion.
	 */
	public int getMinorVersion()
	{
		return minorVersion;
	}
	
	/**
	 * Returns the release version of this build.
	 * @return the release version.
	 */
	public int getReleaseVersion()
	{
		return releaseVersion;
	}

	/**
	 * @return Returns the buildNumber.
	 */
	public String getBuildNumber()
	{
		return buildNumber;
	}
	
	/**
	 * @return Returns the buildSequence.
	 */
	public int getBuildSequence()
	{
		return buildSequence;
	}
	
	/**
	 * @return Returns the message.
	 */
	public String getMessage()
	{
		return message;
	}
	
	/**
	 * @return Returns the messageType.
	 */
	public String getMessageType()
	{
		return messageType;
	}
	
	/**
	 * @return Returns the area.
	 */
	public String getArea()
	{
		return area;
	}
	
	/**
	 * @return Returns the steps.
	 */
	public int getSteps()
	{
		return steps;
	}
	
	/**
	 * @param majorVersion
	 */
	public void setMajorVersion(int majorVersion)
	{
		this.majorVersion = majorVersion;
	}
	
	/**
	 * @param minorVersion
	 */
	public void setMinorVersion(int minorVersion)
	{
		this.minorVersion = minorVersion;
	}
	
	/**
	 * Sets the release version of this build.
	 * @param releaseVersion the new release version for this build.
	 */
	public void setReleaseVersion(int releaseVersion)
	{
		this.releaseVersion = releaseVersion;
	}

	/**
	 * @param buildNumber
	 */
	public void setBuildNumber(String buildNumber)
	{
		this.buildNumber = buildNumber;
	}
	
	/**
	 * @param buildSequence
	 */
	public void setBuildSequence(int buildSequence)
	{
		this.buildSequence = buildSequence;
	}
	
	/**
	 * @param confirmMessage
	 */
	public void setMessage(String confirmMessage)
	{
		this.message = confirmMessage;
	}
	
	/**
	 * @param confirmMessageType
	 */
	public void setMessageType(String confirmMessageType)
	{
		this.messageType = confirmMessageType;
	}
	
	/**
	 * @param area
	 */
	public void setArea(String area)
	{
		this.area = area;
	}
	
	/**
	 * @param steps
	 */
	public void setSteps(int steps)
	{
		this.steps = steps;
	}
	
	public String getId()
	{
		return id;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	public Timestamp getUpgradeDate()
	{
		return upgradeDate;
	}

	public void setUpgradeDate(Timestamp upgradeDate)
	{
		this.upgradeDate = upgradeDate;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final BuildConfig other = (BuildConfig) obj;
		if (buildSequence != other.buildSequence)
			return false;
		if (majorVersion != other.majorVersion)
			return false;
		if (minorVersion != other.minorVersion)
			return false;
		if (releaseVersion != other.releaseVersion)
			return false;
		if (area == null)
		{
			if (other.area != null)
				return false;
		}
		else if (!area.equals(other.area))
			return false;
		if (buildNumber == null)
		{
			if (other.buildNumber != null)
				return false;
		}
		else if (!buildNumber.equals(other.buildNumber))
			return false;
		return true;
	}

	@Override
	public int hashCode()
	{
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((area == null) ? 0 : area.hashCode());
		result = PRIME * result + ((buildNumber == null) ? 0 : buildNumber.hashCode());
		result = PRIME * result + buildSequence;
		result = PRIME * result + majorVersion;
		result = PRIME * result + minorVersion;
		result = PRIME * result + releaseVersion;
		return result;
	}

	@Override
	public String toString()
	{
		return majorVersion + "." + minorVersion + "." + releaseVersion + "." + buildNumber + "-" + buildSequence;
	}
}